package com.wind.util.sms;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class BaseTest {
	public static String url = "http://116.228.55.215:9091/smsserver/api/sms/send";
//	public static String url = "http://10.6.2.24:9091/smsserver/api/sms/send";
//	private   String targetNumber="18930046617";
//	private  String sendNumber="118114";
//	private  String smsInfo="您好，4008服务电话出现异常请查看";
	private   String targetNumber;
	private  String sendNumber;
	private  String smsInfo;
	public String getTargetNumber() {
		return targetNumber;
	}
	public void setTargetNumber(String targetNumber) {
		this.targetNumber = targetNumber;
	}
	public String getSendNumber() {
		return sendNumber;
	}
	public void setSendNumber(String sendNumber) {
		this.sendNumber = sendNumber;
	}
	public String getSmsInfo() {
		return smsInfo;
	}
	public void setSmsInfo(String smsInfo) {
		this.smsInfo = smsInfo;
	}
	public BaseTest(String tmp_targetNumber,String tmp_sendNumber,String tmp_smsInfo) {
		this.setTargetNumber(tmp_targetNumber);
		this.setSendNumber(tmp_sendNumber);
		this.setSmsInfo(tmp_smsInfo);
	}
	public static void main(String[] args) {
//		BaseTest test=new BaseTest("18930046617","118114","您好，您的4008电话出现异常");

		BaseTest test=new BaseTest("18049880825","118114","您好，您的4008电话出现异常");
		test.doSend();
	}
	public String   doSend() {
		String return_str="";
			List<NameValuePair> list = getParamsList();
		try {
			CloseableHttpClient client=getCloseableHttpClient();
			
			
			HttpPost httpPost = new HttpPost(url);
			if(list!=null&&list.size()>0){
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,Charset.forName("UTF-8"));
				httpPost.setEntity(entity);
			}
			
			String result=client.execute(httpPost, responseHandler);
			return_str=result;
			System.out.println(result);
		} catch (Exception e) {
			System.out.println(e);
		}
		return return_str;
	}
	
	
	private ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
		public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
			int status = response.getStatusLine().getStatusCode();
			if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
				HttpEntity entity = response.getEntity();
				
				Charset charset = ContentType.getOrDefault(entity).getCharset();
				if (charset == null) {
					charset = Charset.forName("UTF-8");
				}
				return entity != null ? EntityUtils.toString(entity, charset) : null;
			} else {
				throw new ClientProtocolException("Unexpected response status: " + status);
			}
		}
	};
	
	public CloseableHttpClient getCloseableHttpClient() {
		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(60000)
				.setSocketTimeout(60000)
				.build();
		return HttpClients.custom().setDefaultRequestConfig(config)
									.build();
	}
	
	
	public List<NameValuePair> getParamsList(){
		String appId=Config.SMS_tempUserId;
		String pass=Config.SMS_tempUserPw;
		
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String reqSerialNumber=sdf2.format(new Date());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timestamp=sdf.format(new Date());
		
		TreeMap<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("reqSerialNumber", reqSerialNumber);
		treeMap.put("appId", appId);
		treeMap.put("sendNumber", sendNumber);
		treeMap.put("targetNumber", targetNumber);
		treeMap.put("smsInfo", smsInfo);
		treeMap.put("timestamp", timestamp);
		String sign = ParamsSign.value(treeMap, pass);
		System.out.println("sign:" + sign);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("reqSerialNumber", reqSerialNumber));
		params.add(new BasicNameValuePair("appId", appId));
		params.add(new BasicNameValuePair("sendNumber", sendNumber));
		params.add(new BasicNameValuePair("targetNumber", targetNumber));
		params.add(new BasicNameValuePair("smsInfo", smsInfo));
		params.add(new BasicNameValuePair("timestamp", timestamp));
		params.add(new BasicNameValuePair("sign", sign));
		System.out.println("params:" + params.toString());
		return params;
	}
}
